package com.yoomiito.app.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import cn.droidlover.xdroidmvp.net.ApiException
import com.yoomiito.app.model.bean.UserInfo
import com.yoomiito.app.utils.*
import com.yoomiito.app.utils.LoginUtils.toOneKeyLogin
import com.yoomiito.app.widget.dialog.LoadDialog
import com.yoomiito.app.widget.dialog.NoNetDialog
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

abstract class BaseFragment :Fragment(){
    private val viewModel by viewModels<BaseFragmentViewModel>()
    private lateinit var mNoNetDialog: NoNetDialog
    private lateinit var loadDialog: LoadDialog

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        EventBus.getDefault().register(this)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(getLayoutId(),container,false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initData(savedInstanceState)
    }

    fun initData(savedInstanceState: Bundle?){
        viewModel.getUserInfo.observe(viewLifecycleOwner, Observer {
            setUserInfo(it)
        })
    }

    abstract fun getLayoutId():Int

    override fun onDestroyView() {
        super.onDestroyView()
        EventBus.getDefault().unregister(this)
    }

    private fun showNewDialog(show: Boolean) {
        if (mNoNetDialog == null) {
            mNoNetDialog = NoNetDialog(requireContext())
        }
        if (show) {
            mNoNetDialog.show()
        } else {
            mNoNetDialog.dismiss()
        }
    }

    /**
     * eventbus事件处理
     *
     * @param eventMessage
     */
    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    fun eventMessage(eventMessage: EventMessage) {
        L.i("BaseFragment收到消息：" + eventMessage.eventType)
        if (AllCode.NET_DIALOG_SHOW == eventMessage.eventType) {
            showNewDialog(true)
            L.i("BaseFragment收到消息：显示网络弹窗")
        }
        if (AllCode.NET_DIALOG_DISMISS == eventMessage.eventType) {
            showNewDialog(false)
            L.i("BaseFragment收到消息：取消网络弹窗")
        }
    }

    fun showLoading() {
        if (loadDialog == null) {
            loadDialog = LoadDialog(requireContext())
        }
        loadDialog.show()
    }

    fun dismissLoading() {
        if (loadDialog != null && loadDialog.isShowing) {
            loadDialog.dismiss()
        }
    }

    fun setUserInfo(userInfo: UserInfo?) {
        dismissLoading()
        if (userInfo == null) {
            val context =  requireContext()
            toOneKeyLogin(context)
            if (context is BaseActivity<*>){
                context.finish()
            }
        } else {
            App.mAppConfig.mCurrentUser.mUserInfo = userInfo
            UserInfoConfig.updateRoleId(userInfo)
        }
    }

    fun handleError(e: ApiException) {
        when (e.code) {
            ApiException.TOKEN_OUT_DATA ->
                toOneKeyLogin(requireContext())
            4001 -> {
            }
            else -> YMToast.showText(e.msg)
        }
    }
}